﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using LogLib;

namespace ComLib.ComFun
{
    public class ImageResizing : IDisposable
    {
        private string _srcPath = "";

        private string _dstPath = "";

        private float _xDpi = 300F;

        private float _yDpi = 300F;

        private long _minSize = 20;

        private long _maxSize = 80;

        private long _currentADVal = 120L;

        private Bitmap _srcImage = null;

        public ImageResizing(string src, string dst)
        {
            _srcPath = src;
            _dstPath = dst;
            _srcImage = new Bitmap(_srcPath);
        }

        public bool OnHandling()
        {
            if (!ToJpeg(_currentADVal))
            {
                return false;
            }
            long length = GetDstFileLength();
            if (length < _minSize * 1024)
            {
                ResizeImage(_dstPath);
                return true;
            }
            if (length > _maxSize * 1024)
            {
                _currentADVal -= 5;
                if (_currentADVal > 0)
                {
                    return OnHandling();
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        private long GetDstFileLength()
        {
            long length = 0;
            using (FileStream fs = new FileStream(_dstPath, FileMode.Open))
            {
                length = fs.Length;
            }
            return length;
        }

        /// <summary>
        /// 将Bmp转换为JPEG
        /// </summary>
        /// <param name="strSrc">Bmp文件路径</param>
        /// <param name="strDst">JPEG保存路径</param>
        /// <returns></returns>
        private bool ToJpeg(long ADVal)
        {           
            bool bRet = false;
            Bitmap newImg = new Bitmap(_srcImage.Width, _srcImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            try
            {
                newImg.SetResolution(_xDpi, _yDpi);
                using (Graphics graphic = Graphics.FromImage(newImg))
                {
                    //设置高质量查值法
                    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    //设置高质量，低速度呈现平滑程度
                    graphic.SmoothingMode = SmoothingMode.HighQuality;
                    //清空画布并以透明背景色填充
                    graphic.Clear(System.Drawing.Color.Transparent);
                    graphic.DrawImage(_srcImage,
                        new Rectangle(0, 0, newImg.Width, newImg.Height),
                        new Rectangle(0, 0, newImg.Width, newImg.Height),
                        System.Drawing.GraphicsUnit.Pixel);
                }
                ImageCodecInfo[] vImageCodecInfos = ImageCodecInfo.GetImageEncoders();
                foreach (ImageCodecInfo vImageCodecInfo in vImageCodecInfos)
                {
                    if (vImageCodecInfo.FormatDescription.ToLower() == "jpeg")
                    {
                        EncoderParameters vEncoderParameters = new EncoderParameters(1);
                        vEncoderParameters.Param[0] = new EncoderParameter(
                            System.Drawing.Imaging.Encoder.Quality, ADVal);
                        newImg.Save(_dstPath, vImageCodecInfo, vEncoderParameters);
                        break;
                    }
                }
                bRet = true;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
            }
            finally
            {
                if (newImg != null)
                {
                    newImg.Dispose();
                    newImg = null;
                }               
            }
            return bRet;
        }

        public static void ResizeImage(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            byte[] b = { 0x00 };
            while (fs.Length < 22 * 1024)
            {
                fs.Write(b, 0, b.Length);
            }
            fs.Flush();
            fs.Close();
        }


        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
